<?php

namespace App\Http\Controllers\Resource;

use Illuminate\Support\Facades\Log;

/**
 * 博看 数字资源
 *
 * 未提供 目录接口
 */
class BkController extends CommonController
{
    public $instanceId = 'yzqlib';
    public $sign = 'e10adc3949ba59abbe56e057f20f883e';
    public $static_key = 'UysHdlxnXW0EclAwCCRablAzU2wFNwYhAi5VJVpvVyMAZFEgUztVIwJkBGoNM1diUSYMLFd2AGtUNQE+BjlQdFNqB3Zcd11zBCFQZghvWj5QKFMoBW0GIQIuVSVadFc4AG5RZ1MjVTsCZwRrDTRXZVE9DDJXYQA9VGwBZgYh';



    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分类接口
     *
     * @param https://oauth.bookan.com.cn/openapi/libraryCategoryList?sign=e10adc3949ba59abbe56e057f20f883e&instanceId=cqtsg&type=1
     *
     * @param type  1:期刊   3：图书
     */
    public function getTypeList($type)
    {
        $url = 'https://oauth.bookan.com.cn/openapi/libraryCategoryList';
        $url = $url . '?sign=' . $this->sign . '&instanceId=' . $this->instanceId . '&type=' . $type;

        $result = request_url($url);
        $result = json_decode($result, true);
        if (!isset($result['code']) || $result['code'] !== 0) {
            Log::channel('bklog')->debug('资源获取失败');
            Log::channel('bklog')->debug($url);
            Log::channel('bklog')->debug($result);
            Log::channel('bklog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }



    /**
     * 获取图书数据
     * @param type  1:期刊   3：图书
     * @param type_id 分类id
     * @param page 分页
     * @param limit 每页显示条数 最大只能是50
     */
    public function getBookList($type, $type_id, $page = 1, $limit = 50)
    {
        $url = 'https://oauth.bookan.com.cn/openapi/libraryData';
        $url = $url . '?sign=' . $this->sign . '&instanceId=' . $this->instanceId . '&type=' . $type . '&categoryId=' . $type_id . '&pageNum=' . $page . '&limitNum=' . $limit;

        $result = request_url($url);
        $result = json_decode($result, true);

        if (!isset($result['code']) || $result['code'] !== 0) {
            Log::channel('bklog')->debug('资源获取失败');
            Log::channel('bklog')->debug($url);
            Log::channel('bklog')->debug($result);
            Log::channel('bklog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        if (empty($result['data'])) {
            return [];
        }
        return  $result;
    }



    /**
     * 单点登录
     * @param  username String 授权登录传递用户身份标识：登录用户名、手机号、用户ID等均可，保证系统中的唯一性
     * @param  url String 需要跳转的url地址。详情接口返回的地址
     */
    public function singleSign($username, $readUrl = null)
    {
        $url = 'https://oauth.bookan.com.cn/openapi/libraryDataOauthLogin';

        $time = date('Y-m-d');
        $sign = md5($username . $time . $this->static_key);
        $url = $url . '?sign=' . $sign . '&instanceId=' . $this->instanceId . '&userName=' . $username . '&readUrl=' . urlencode($readUrl);
        return  $url;
    }




    /**
     * 字典序排序
     */
    public function arraySort(array $param)
    {
        ksort($param);
        reset($param);

        return $param;
    }
}
